﻿using System;
using System.Web.Mvc;
using Service.Infrastructure;
using Service.Infrastructure.Implement;
using Service.Infrastructure.Interface;
using Service.Infrastructure.Result;
using Web.Controllers;

namespace Web.Areas.Sys.Controllers
{
    public class LoginController : Base
    {

        protected readonly IAccount Manager;

        public LoginController()
        {
            Manager = new ManagerService();
        }

        // GET: /Sys/Login/
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult LoginIn(String userName, String password)
        {
            return LoginIn(Manager.Login(userName, password));
        }

        private ActionResult LoginIn(ServiceResult result)
        {
            if (LoginSuccess(result))
            {
                //设置Session
                Session[ManagerSessionKey] = result.ReData;
                return RedirectToAction("Index","Admin");
            }

            TempData[ErrorInfo] = result.GetMsg();
            return View();
        }

        private bool LoginSuccess(ServiceResult result)
        {
            return (ResultType) result.ReCode == ResultType.AccountLoginSuccess;
        }

        public ActionResult Registration()
        {
            return RedirectToAction("Index");
        }

        public ActionResult ForgetPassword()
        {
            return RedirectToAction("Index");
        }

    }
}
